/*
 * FindBugs - Find Bugs in Java programs
 * Copyright (C) 2003-2008 University of Maryland
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package edu.umd.cs.findbugs.gui2;

import edu.umd.cs.findbugs.gui2.sort.SortableBugTree;
import edu.umd.cs.findbugs.gui2.sort.SorterTableColumnModel;

/**
 * Subclass of the normal BugTreeModel, which extracts all the static caching which has bad effects
 * when there's more than one {@link BugTreeModel} instance in the application. Should provide an
 * easier path to removing static caches altogether, if that becomes necessary.
 * 
 * 
 * @author Graham Allan (grallan@cis.strath.ac.uk)
 */
public class CachedBugTreeModel extends BugTreeModel {

	public CachedBugTreeModel(SortableBugTree tree, SorterTableColumnModel st, BugSet data, FilterActivity filterActivity) {
		super(tree, st, data, filterActivity);
		BugSet.setAsRootAndCache(bugSet, st.getOrderAfterDivider());
		
	}

	public CachedBugTreeModel(BugTreeModel other) {
		super(other);
		BugSet.setAsRootAndCache(bugSet, sorter.getOrderAfterDivider());
	}

	@Override
	public void changeSet(BugSet set) {
		BugSet.setAsRootAndCache(set, sorter.getOrderAfterDivider());
		super.changeSet(set);
	}
	

	@Override
	protected FilterListener filterListenerToAdd() {
		return new CachingFilterListener();
	}

	protected class CachingFilterListener extends MyFilterListener {
		@Override
		public void clearCache() {
			if (TRACE)
				System.out.println("clearing cache in bug tree model");
			resetData();
			BugSet.setAsRootAndCache(bugSet, sorter.getOrderAfterDivider());// FIXME: Should this be in
			// resetData? Does this
			// allow our main list to
			// not be the same as the
			// data in our tree?
			root.setCount(bugSet.size());

			rebuild();
		}
	}

	@Override
	protected CachedBugTreeModel clone() {
		return new CachedBugTreeModel(this);
	}
	
}
